package php4dvdPages;


import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import php4dvdTests.MyPageFactory;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

/**
 * @author Anna Alexeyenko
 *         Date: 1/26/13
 */
public class InternalPage extends AnyPage {

    @FindBy(xpath = ".//header//a[@href = \"http://localhost/php4dvd/\"]")
    private WebElement homeMenuItem;

    @FindBy(xpath = "//header//a[contains(@href, \"/php4dvd/?go=profile\")]")
    private WebElement myProfileMenuItem;

    @FindBy(xpath = "//header//a[contains(@href, \"/php4dvd/?go=users\")]")
    private WebElement userManagementMenuItem;

    @FindBy(xpath = "//header//a[contains(@href, \"/php4dvd/?logout\")]")
    private WebElement logoutMenuItem;

    public LoginPage logout() {
        logoutMenuItem.click();
        waitForElement().until(alertIsPresent()).accept();
        return MyPageFactory.getPage(driver, LoginPage.class);
    }

    public ProfilePage goToMyProfilePage() {
        myProfileMenuItem.click();
        return MyPageFactory.getPage(driver, ProfilePage.class);
    }

    public UserManagementPage goToUserManagementPage() {
        userManagementMenuItem.click();
        return MyPageFactory.getPage(driver, UserManagementPage.class);
    }

    public HomePage goToHomePage() {
        homeMenuItem.click();
        return MyPageFactory.getPage(driver, HomePage.class);
    }

    @Override
    public boolean isOnThisPage() {
        return logoutMenuItem.isDisplayed();
    }

    @Override
    public void tryToOpen() {
        MyPageFactory.getPage(driver, LoginPage.class).loginByDefault();
    }


}
